package com.excel.exceldemo.service.impl;

import com.alibaba.excel.EasyExcel;
import com.excel.exceldemo.mapper.UserMapper;
import com.excel.exceldemo.pojo.User;
import com.excel.exceldemo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public int addUserBatch(@RequestPart(value = "file") MultipartFile file) throws IOException {
        log.info("开始执行导入数据方法");
        List<User> excels= EasyExcel.read(file.getInputStream()).head(User.class).sheet().doReadSync();
        int a=userMapper.batchInserUser(excels);
        log.info("导入数据成功");
        return a;
    }

    @Override
    public List<User> getUserList() {
        log.info("开始执行导出数据为excle业务");
        List<User> users = userMapper.selectUserList();
        log.info("文件内容{}",users.toString());
        return users;
    }
}
